<template>
  <view class="work-container">
    <u-navbar placeholder autoBack>
      <view slot="center" style="width: 70vw">
        <TopBar :tabBars="tabBars" :tabIndex="tabIndex" @change="onTabChange"></TopBar>
      </view>
      <view slot="right">
        <i class="iconfont icon-shaixuan" style="font-size: 40rpx" @click="handleNavigationBarButtonTap()" />
      </view>
    </u-navbar>

    <book-list :listData="listData"></book-list>

    <CuFilter ref="allCuFilter" :showArr="allFilterShowArr" navigationStyle="custom" @submit="allFilterSubmit" />
    <CuFilter ref="myCuFilter" :showArr="myFilterShowArr" navigationStyle="custom" :showCreateBy="false" @submit="myFilterSubmit" />
  </view>
</template>

<script>
import { getHiddenDangerList } from '../../../api/dbd/hiddenDangerBook'
import TopBar from '@/components/top-bar/index.vue'
import CuFilter from '@/components/cu-filter/cu-filter.vue'
import BookList from './components/bookList.vue'
import dayjs from 'dayjs'
import store from '../../../store'
import { mapGetters } from 'vuex'
import UNavbar from '@/uni_modules/uview-ui/components/u-navbar/u-navbar.vue'
export default {
  components: {
    UNavbar,
    TopBar,
    BookList,
    CuFilter
  },
  computed: {
    ...mapGetters(['staffId'])
  },
  data() {
    return {
      isFinished: false,
      tabBars: [
        {
          name: '全部',
          componentName: 'all',
          index: 0
        },
        {
          name: '我的',
          componentName: 'mine',
          index: 1
        }
      ],
      tabIndex: 0,
      pageNum: 1,
      total: null,
      listData: [],
      allFilterShowArr: ['managerType', 'managerStatus', 'managerSource', 'selectUser', 'selectTime'],
      allSearchParams: undefined,
      myFilterShowArr: ['managerType', 'managerStatus', 'managerSource', 'selectUser', 'selectTime'],
      mySearchParams: undefined
    }
  },
  onPullDownRefresh() {
    this.initData()
  },
  onReachBottom() {
    this.pageNum++
    this.getList()
  },
  onShow() {
    this.initData()
  },
  methods: {
    handleNavigationBarButtonTap() {
      if (this.tabIndex === 0) {
        this.$refs.allCuFilter.toggle()
      } else {
        this.$refs.myCuFilter.toggle()
      }
    },
    allFilterSubmit(val) {
      console.log(val)
      this.pageNum = 1
      this.total = null
      this.allSearchParams = {}
      if (val.managerType) this.allSearchParams.managerType = val.managerType
      if (val.managerStatus) this.allSearchParams.mangerStatus = val.managerStatus
      if (val.managerSource) this.allSearchParams.managerSource = val.managerSource

      if (val.createBy) this.allSearchParams.reportPersonId = val.createBy
      if (val.reviewPersonId) this.allSearchParams.actualReviewPersonId = val.reviewPersonId
      if (val.chargePersonId) this.allSearchParams.actualChargePersonId = val.chargePersonId
      if (val.acceptancePersonId) this.allSearchParams.actualAcceptancePersonId = val.acceptancePersonId

      if (val.date[0]) this.allSearchParams.reportDateFrom = dayjs(val.date[0]).format('YYYY-MM-DD')
      if (val.date[1]) this.allSearchParams.reportDateTo = dayjs(val.date[1]).format('YYYY-MM-DD')
      this.getAll()
    },
    myFilterSubmit(val) {
      this.pageNum = 1
      this.total = null
      this.mySearchParams = {}
      if (val.managerType) this.mySearchParams.managerType = val.managerType
      if (val.managerStatus) this.mySearchParams.mangerStatus = val.managerStatus
      if (val.managerSource) this.mySearchParams.managerSource = val.managerSource

      if (val.reviewPersonId) this.mySearchParams.actualReviewPersonId = val.reviewPersonId
      if (val.chargePersonId) this.mySearchParams.actualChargePersonId = val.chargePersonId
      if (val.acceptancePersonId) this.mySearchParams.actualAcceptancePersonId = val.acceptancePersonId

      if (val.date[0]) this.mySearchParams.reportDateFrom = dayjs(val.date[0]).format('YYYY-MM-DD')
      if (val.date[1]) this.mySearchParams.reportDateTo = dayjs(val.date[1]).format('YYYY-MM-DD')
      this.getMyHandleList()
    },
    initData() {
      this.pageNum = 1
      this.isFinished = false
      this.getList()
    },
    onTabChange(obj) {
      if (this.tabIndex === obj.index) {
        return
      }
      this.tabIndex = obj.index
      this.listData = []
      this.initData()
    },
    getList() {
      if (this.isFinished) {
        return
      }
      if (this.tabIndex === 1) {
        this.getMyHandleList()
      } else {
        this.getAll()
      }
    },
    getAll() {
      const req = {
        pageNum: this.pageNum,
        ...this.allSearchParams
      }
      getHiddenDangerList(req)
        .then((res) => {
          this.listData = this.pageNum === 1 ? res.rows : [...this.listData, ...res.rows]
          this.isFinished = this.listData.length === res.total
        })
        .finally(() => {
          uni.stopPullDownRefresh()
        })
    },
    getMyHandleList() {
      const req = {
        pageNum: this.pageNum,
        ...this.mySearchParams,
        reportPersonId: store.getters.staffId
      }
      getHiddenDangerList(req)
        .then((res) => {
          this.listData = this.pageNum === 1 ? res.rows : [...this.listData, ...res.rows]
          this.isFinished = this.listData.length === res.total
        })
        .finally(() => {
          uni.stopPullDownRefresh()
        })
    }
  }
}
</script>
